package com.example.opengldemo.widget;

import android.content.Context;
import android.opengl.GLES10;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.util.AttributeSet;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MyGlSurfaceView extends GLSurfaceView implements GLSurfaceView.Renderer {
    public MyGlSurfaceView(Context context) {
        this(context, null);
    }

    public MyGlSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setEGLContextClientVersion(1);
        setRenderer(this);
    }

    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES10.glViewport(0, 0, width, height);//设置视口宽度高度
        //修改投影矩阵
        GLES10.glMatrixMode(GLES10.GL_PROJECTION);
        GLES10.glLoadIdentity();// 复位，将投影矩阵归零
        GLU.gluPerspective(gl, 60.0f, 1.0f * width / height, 1.0f, 300.0f);

    }


    @Override
    public void onDrawFrame(GL10 gl10) {
        //清空场景为黑色
        GLES10.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        //清空相关缓存
        GLES10.glClear(GLES10.GL_COLOR_BUFFER_BIT | GLES10.GL_DEPTH_BUFFER_BIT);
        GLES10.glEnableClientState(GLES10.GL_VERTEX_ARRAY);
        GLES10.glDrawArrays(GLES10.GL_TRIANGLES, 0, 3);
        GLES10.glFlush();
    }
}
